package cn.wolfcode.ssm.util;

import cn.wolfcode.ssm.domain.Employee;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

public class UserContext {
    public static final String PERMISSIONS_IN_SESSION = "PERMISSIONS_IN_SESSION";
    public static final String USER_IN_SESSION = "USER_IN_SESSION";
    public static final String ERROR_TIME_IN_SESSION = "ERROR_TIME_IN_SESSION";

    //获取Session对象
    public static HttpSession getSession(){
        ServletRequestAttributes attrs = (ServletRequestAttributes)RequestContextHolder.currentRequestAttributes();
        return attrs.getRequest().getSession();
    }

    //权限表达式的设置进Session的方法set
    public static void setPermission(List<String> expressions){
        getSession().setAttribute(PERMISSIONS_IN_SESSION,expressions);
    }
    //从Session 中获取权限表达式集合的方法get
    public static List<String> getPermission(){
        return (List<String>)getSession().getAttribute(PERMISSIONS_IN_SESSION);
    }
    //员工信息的设置进Session的方法set
    public static void setEmployee(Employee employee){
        getSession().setAttribute(USER_IN_SESSION,employee);
    }

    //从Session 中获取员工信息的方法get
    public static Employee getEmployee(){
        return (Employee) getSession().getAttribute(USER_IN_SESSION);
    }

    //
    public static Integer getErrorTime(){
        Object errorTime = getSession().getAttribute(ERROR_TIME_IN_SESSION);
        if(errorTime !=null){
            return (Integer)errorTime;
        }
        return null;
    }

    //
    public static void setErrorTime(int errorTime){
        getSession().setAttribute(ERROR_TIME_IN_SESSION,errorTime);
    }
}
